#include "DS1302.h"
#include <REGX52.H>
#include "LCD1602.h"
#include "DelayTime.h"
#include "KeyBoard.h"
#include "Timer.h"


#define SHOW_MODE 0
#define SET_MODE 1
unsigned char Selected = 0;
unsigned char Show_Time_Idx[] = {1,4,7};
unsigned char Flash_Flag = 0; // 闪烁标志位，通过定时器控制

void test()
{
	unsigned char Data = 5;
	//0000 1010
	DS1302_WriteByte(0x80,(Data / 10 * 16) + (Data % 10));
	Data = DS1302_ReadByte(0x81);
	LCD_ShowNum(1,1,(Data / 16 * 10)+(Data % 16),2);
}

void Show_Time()
{
	LCD_ShowNum(1,1,time[YEAR_IDX],2);
	LCD_ShowNum(1,4,time[MON_IDX],2);
	LCD_ShowNum(1,7,time[DATE_IDX],2);
	LCD_ShowNum(2,1,time[HOUR_IDX],2);
	LCD_ShowNum(2,4,time[MIN_IDX],2);
	LCD_ShowNum(2,7,time[SEC_IDX],2);
}

void Check_Time()
{
	unsigned char mod,cur_mon;
	unsigned char days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	// 设置时间时判断时间是否合法
	//0-99
	if(Selected == YEAR_IDX)
	{
		if(time[YEAR_IDX] == 100)
			time[YEAR_IDX] = 0;
		else if((time[YEAR_IDX] == -1))
			time[YEAR_IDX] = 99;
	}
	else if(Selected == MON_IDX)
	{
		// 1-12   0,13
		if(time[MON_IDX] == 13)
			time[MON_IDX] = 1;
		else if((time[MON_IDX] == 0))
			time[MON_IDX] = 12;
	}
	else if(Selected == DATE_IDX)
	{
		cur_mon = time[MON_IDX];
		// 1-days[MON_IDX]
		if(cur_mon == 2 && time[YEAR_IDX] % 4 == 0 )
		{
			days[cur_mon] = 29;
		}
		// 1 - 30
		if(time[DATE_IDX] == days[cur_mon] + 1)
			time[DATE_IDX] = 1;
		else if((time[DATE_IDX] == 0))
			time[DATE_IDX] = days[cur_mon];
	}
	else if(Selected == HOUR_IDX)
	{
		if(time[HOUR_IDX] == 25)
			time[HOUR_IDX] = 0;
		else if(time[HOUR_IDX] == -1)
			time[HOUR_IDX] = 24;
		
	}
	else if(Selected == MIN_IDX)
	{
		if(time[MIN_IDX] == 61)
			time[MIN_IDX] = 0;
		else if(time[MIN_IDX] == -1)
			time[MIN_IDX] = 60;
	}
	else if(Selected == SEC_IDX)
	{
		if(time[SEC_IDX] == 61)
			time[SEC_IDX] = 0;
		else if(time[SEC_IDX] == -1)
			time[SEC_IDX] = 60;
	}
}

// 按键检测
void Set_Time(unsigned char key_num)
{
	int row,col;
	if(key_num == 2)
	{
		Selected++;
		Selected %= DAY_IDX;
	}
	else if(key_num == 3)
	{
		time[Selected]++;
	}
	else if(key_num == 4)
	{
		time[Selected]--;
	}
	

	// 需要把设置的时间重新在LCD上显示
	// 1.检测修改后的时间是否正确
	Check_Time();
	// 2.再次刷新
	row = Selected / 3 + 1;
	col = Show_Time_Idx[Selected % 3];
	
	if(Selected == YEAR_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}

	if(Selected == MON_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}

	if(Selected == DATE_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}

	if(Selected == HOUR_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}
	
	if(Selected == MIN_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}

	if(Selected == SEC_IDX && Flash_Flag == 1) {LCD_ShowString(row,col,"  ");}
	else {LCD_ShowNum(row,col,time[Selected],2);}
	

}


void main()
{
	unsigned char Data = 0;
	unsigned char MODE = SHOW_MODE;
	int key_num = -1;

	DS1302_Init();
	LCD_Init();
	TimerInit();
	
	// 24-11-14
	// 21:32:52
	LCD_ShowString(1,1,"  -  -");
	LCD_ShowString(2,1,"  :  :");
	Write_Time();
	while(1)
	{
		// 实现的现象：根据MODE，去执行对应的功能
		// 按键0， 按一次更换一次模式
		// MODE: SHOW,则显示
		// MODE: SET,根据按键，去执行对应的操作
		key_num = Key_Num();
		if(key_num == 1)
		{
			if(MODE == SHOW_MODE) // 功能转换
			{
				MODE = SET_MODE;
				Selected = 0;
			}
			else if(MODE == SET_MODE)
			{
				MODE = SHOW_MODE;
				// 再要进入SHOW_MODE前，先把之前更新的时间设置进DS1302内
				Write_Time();
			}
		}
		switch (MODE)
		{
		case SHOW_MODE:
			// 读取并显示时间
			Read_Time();
			Show_Time();
			break;

		case SET_MODE:
			// 时间设置，通过按键更新时间
			Set_Time();
			break;

		default:
			break;
		}
		
		LCD_ShowNum(2,11,Selected,1);
		// 以下代码有问题，会阻塞读取DS1302的数据
		// if(key_num == 0)
		// {
		// 	MODE = !MODE;
		// }
		// else if(key_num == 1)
		// {
		// 		Selected++;
		// 		Selected %= DAY_IDX;
		// }
		// else if(key_num == 2)
		// {
		// 	time[Selected]++;
		// }
		// else if(key_num == 3)
		// {
		// 	time[Selected]--;
		// }		
		// if(MODE == SET_MODE)
		// 	LCD_ShowNum(2,11,Selected,1);
		// LCD_ShowNum(1,11,key_num,1);
		// LCD_ShowNum(1,13,MODE,1);
		// LCD_ShowNum(1,15,Selected,1);
		// switch(MODE)
		// {
		// 	case SHOW_MODE:
		// 	{
		// 		Get_Time();
		// 		Show_Time();
		// 		Delayxms(50);
		// 	}
		// 	case SET_MODE:
		// 	{
		// 		Selected = 0;
		// 		Check_Time();
		// 		Set_Time();
		// 	}
		// }
	}
}
// 中断函数的入口 
// 每隔50毫秒，显示一次选中的数组
void Timer0_Routine(void)		interrupt 1
{
	static int countT = 0; 
	TL0 = 0x18;		
	TH0 = 0xFC;	
	countT++;
	if(countT >= 500)
	{
		Flash_Flag = !Flash_Flag;
		countT = 0;
	}	
}
